package cn.yudao.product.service.impl;

import cn.yudao.basic.exception.BusinessException;
import cn.yudao.basic.utils.CodeGenerateUtils;
import cn.yudao.order.domain.OrderPetAcquisition;
import cn.yudao.order.domain.OrderProductAcquisition;
import cn.yudao.order.mapper.OrderProductAcquisitionMapper;
import cn.yudao.org.domain.Employee;
import cn.yudao.pet.domain.Pet;
import cn.yudao.pet.domain.SearchMasterMsg;
import cn.yudao.pet.dto.AcceptDto;
import cn.yudao.product.domain.Product;
import cn.yudao.product.domain.ProductPay;
import cn.yudao.product.domain.ProductPayLog;
import cn.yudao.product.mapper.ProductMapper;
import cn.yudao.product.mapper.ProductPayLogMapper;
import cn.yudao.product.mapper.ProductPayMapper;
import cn.yudao.product.service.IProductPayService;
import cn.yudao.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yudao
 * @since 2022-07-21
 */
@Service
public class ProductPayServiceImpl extends BaseServiceImpl<ProductPay> implements IProductPayService {

    @Autowired
    private ProductPayMapper productPayMapper;

    @Autowired
    private ProductPayLogMapper productPayLogMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private OrderProductAcquisitionMapper orderProductAcquisitionMapper;

    @Override
    public void reject(Long id) {
        ProductPay productPay = productPayMapper.loadById(id);
        productPay.setState(-1);
        productPayMapper.upadte(productPay);
        Product product = productMapper.loadById(productPay.getProId());
        // 生成日志
        ProductPayLog productPayLog = new ProductPayLog();
        productPayLog.setShopId(product.getShopId());
        productPayLog.setState(-1);
        productPayLog.setProductId(productPay.getProId());
        productPayLog.setAuditTime(new Date());
        productPayLog.setNote("拒绝接单");
        productPayLogMapper.save(productPayLog);
    }

    @Override
    public void acceptOrder(AcceptDto acceptDto) {
            // 非空判断
            Long msgId = acceptDto.getMsgId(); // 寻主信息id
            Long handler = acceptDto.getHandler(); // 接单员工id
            String note = acceptDto.getNote(); // 备注信息
            if (msgId == null || handler == null || StringUtils.isEmpty(note))
                throw new BusinessException("信息为空，请重试");
            // 先查询数据库中是否存在当前的订单表根据寻主信息的id查询
            OrderProductAcquisition orderProductAcquisition = orderProductAcquisitionMapper.loadByProPayId(msgId);
        ProductPay productPay = productPayMapper.loadById(msgId);
        // 如果订单不存在或者订单状态为-1则表示可以接单
            if (orderProductAcquisition != null){
                throw new BusinessException("订单已被领取");
            }
            // 改变当前订单状态
        productPay.setState(1);
        productPayMapper.upadte(productPay);
        OrderProductAcquisition orderProductAcquisition1 = new OrderProductAcquisition();
        Product product = productMapper.loadById(productPay.getProId());
        // 添加订单信息
        String orderSn = CodeGenerateUtils.generateOrderSn(productPay.getUserId());
        // 赋值
        orderProductAcquisition1.setShopId(product.getShop().getId());
        System.out.println(product.getShopId());
        orderProductAcquisition1.setOrderSn(orderSn);
        orderProductAcquisition1.setLastcomfirmtime(new Date(System.currentTimeMillis() + 2*24*60*60));
        orderProductAcquisition1.setUserId(productPay.getUserId());
        orderProductAcquisition1.setTProductPayId(productPay.getId());
        orderProductAcquisition1.setEmpId(handler);
        orderProductAcquisition1.setDigest(note);
        orderProductAcquisition1.setState(0);
        orderProductAcquisitionMapper.save(orderProductAcquisition1);


    }
}
